﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using System.IO.IsolatedStorage;
using System.IO;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using GameStateManagement;
using System.Net.Sockets;
using System.Net;

namespace PiratesOfBermuda
{
    public class ClientGameScreen : GameScreen
    {
        #region Initialize
        // Lưu ý biến gameProcessor duoc khai bao trong file GameScreen

        public ClientGameScreen()
        {
            // Chi nhan tu touch sreen dong tac nhan
            EnabledGestures = GestureType.Tap;       
        }

        public override void LoadContent()
        {
            gameProcessor = new ClientGameProcessor(ScreenManager.Game, ScreenManager.SpriteBatch);
            gameProcessor.playerLifeRemained = gameProcessor.opponentLifeRemained = 2;
            gameProcessor.backgroundTexture = Load<Texture2D>(@"Textures/Backgrounds/GameplayBackground");
            gameProcessor.explodeTexture = Load<Texture2D>(@"Textures/MainGameplay/cannonblast");
            base.LoadContent();
        }
        #endregion               

        #region UPDATE
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            gameProcessor.Update(gameTime);

            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }
        #endregion

        #region DRAW
        public override void Draw(GameTime gameTime)
        {
            gameProcessor.Draw(gameTime, ScreenManager.font);
        }
        #endregion

        #region Player Helper Method
        protected void PauseCurrentGame()
        {
            var pauseMenuBackground = new BackgroundScreen();

            ScreenManager.AddScreen(pauseMenuBackground);

            /// @Duy Quang: Luu y doi voi SinglePlayerScreen thi:
            /// ScreenManager.AddScreen(new PauseScreen(this, pauseMenuBackground, true));
            ScreenManager.AddScreen(new PauseScreen(this, pauseMenuBackground, false));
        }

        /// <summary>
        /// Xu ly thong tin nhan duoc tu nguoi choi
        /// </summary>
        /// <param name="input"></param>
        public override void HandleInput(InputState input)
        {
            if (input == null)
                throw new ArgumentNullException("input");
            if (input.IsPauseGame())
            {
                PauseCurrentGame();
            }
            gameProcessor.HandleInput(input.Gestures);
            if (gameProcessor.gameState != GameState.Playing)
            {
                foreach (GestureSample gestureSample in input.Gestures)
                {
                    if (gestureSample.GestureType == GestureType.Tap)
                    {
                        ScreenManager.AddScreen(new GameOverScreen(this, true));
                        ((MultiPlayerGameProcessor)gameProcessor).Channel.Close();
                    }
                }
            }
        }
        #endregion
    }
}
